home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / zendisk2.zip / LST11-15.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  2KB  |  77 lines

  1. ;
  2. ; *** Listing 11-15 ***
  3. ;
  4. ; Finds the last non-blank character in a string, using
  5. ; LODSW and checking 2 bytes per read.
  6. ;
  7.     jmp    Skip
  8. ;
  9. TestString    label    byte
  10.     db    'This is a test string with blanks....'
  11.     db    '                                     ',0
  12. ;
  13. ; Finds the last non-blank character in the specified
  14. ; zero-terminated string.
  15. ;
  16. ; Input:
  17. ;    DS:SI = zero-terminated string to search
  18. ;
  19. ; Output:
  20. ;    SI = pointer to last non-blank character in string,
  21. ;        or 0 if there are no non-blank characters in
  22. ;        the string
  23. ;
  24. ; Registers altered: AX, BL, DX, SI
  25. ;
  26. ; Direction flag cleared
  27. ;
  28. ; Note: Do not pass a string that starts at offset 0 (SI=0),
  29. ;    since a return pointer to the first byte and failure
  30. ;    to find a non-blank character would be
  31. ;    indistinguishable.
  32. ;
  33. ; Note: Does not handle strings that are longer than 64K
  34. ;    bytes or cross segment boundaries.
  35. ;
  36. FindLastNonBlankInString:
  37.     mov    dx,1    ;so far we haven't found a non-blank
  38.             ; character
  39.     mov    bl,' '    ;put our search character, the space
  40.             ; character, in a register for speed
  41.     cld
  42. FindLastNonBlankInStringLoop:
  43.     lodsw        ;get the next 2 string bytes
  44.     and    al,al    ;is the first byte the terminating
  45.             ; zero?
  46.     jz    FindLastNonBlankInStringDone
  47.             ;yes, we're done
  48.     cmp    al,bl    ;is the second byte a space?
  49.     jz    FindLastNonBlankInStringNextChar
  50.             ;yes, so check the next character
  51.     mov    dx,si    ;remember where the non-blank was
  52.     dec    dx    ;adjust back to first byte of word
  53. FindLastNonBlankInStringNextChar:
  54.     and    ah,ah    ;is the second byte the terminating
  55.             ; zero?
  56.     jz    FindLastNonBlankInStringDone
  57.             ;yes, we're done
  58.     cmp    ah,bl    ;is the second byte a space?
  59.     jz    FindLastNonBlankInStringLoop
  60.             ;yes, so check the next 2 bytes
  61.     mov    dx,si    ;remember where the non-blank was
  62.     jmp    FindLastNonBlankInStringLoop
  63.             ;check the next 2 bytes
  64. FindLastNonBlankInStringDone:
  65.     dec    dx    ;point back to the last non-blank
  66.             ; character, correcting for the
  67.             ; 1-byte overrun of LODSW
  68.     mov    si,dx    ;return pointer to last non-blank
  69.             ; character in SI
  70.     ret
  71. ;
  72. Skip:
  73.     call    ZTimerOn
  74.     mov    si,offset TestString     ;string to search
  75.     call    FindLastNonBlankInString ;search for the byte
  76.     call    ZTimerOff
  77.